ci: Format nixfmt command prominently#373939
Conversation
The latter part is structurally a full sentence, if short.
#363759 :D |
|
Successfully created backport PR for |
|
@infinisil, not sure.
That seems either unsuitable or insufficiently documented.
Not what I want. I want to rewrite commits between
Marking commits? I don't see why I would need that.
Not my use case. I just want to format my own commits. It's not a rebase. |
|
Setting up nixfmt as a pre-commit hook might help. At least eventually, when all files have been formatted once. Right now, you'd end up with random reformats all the time. |
In I'm trying this now. It requires the arguments from the formatting error to be passed to the new script: #!/usr/bin/env bash
help='
Usage: format-commits <UPSTREAM> -- FILES...
Format the commits between the current branch and <UPSTREAM>.
'
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
echo "$help"
exit 0
;;
--)
shift
break
;;
*)
break
;;
esac
shift
done
if [ $# -lt 2 ]; then
echo "$help"
exit 1
fi
upstream="$1"
shift
files=( "$@" )
}
get_commits() {
git log --oneline "$merge_base"..HEAD -- ${files[@]}
}
checks() {
if ! type nixfmt &>/dev/null; then
echo "nixfmt is not on PATH. Try again in nix-shell or nix develop."
exit 1
fi
}
main() {
parse_args "$@"
# set -x
checks
merge_base=$(git merge-base HEAD "$upstream")
echo "The merge base is $merge_base."
commits=$(get_commits)
if [ -z "$commits" ]; then
echo "No commits to format."
exit 0
fi
if [ -z "$files" ]; then
echo "No files to format. Pass the files to format as arguments."
exit 0
fi
echo "The commits between $upstream and HEAD will be formatted:"
# This way git shows it in color
get_commits
read -p "Continue? [y/N] "
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo
echo "Aborted."
exit 1
fi
# FIXME: Add `--ignore-missing` to nixfmt
# cp $(dirname ${BASH_SOURCE[0]})/format-commits-helper-optionally.sh /tmp/format-commits-helper-optionally.sh
FILTER_BRANCH_SQUELCH_WARNING=1 \
git filter-branch --tree-filter "/tmp/format-commits-helper-optionally.sh ${files[*]@Q}" "$merge_base"..HEAD
}
main "$@"
|
Fair enough, I thought it would be similar enough to share the same code, but I guess not (or very little) :) |
|
Soon we'll do a treewide reformat of all files, after which it'll be possible to run |
Formatting it prominently makes it easy to apply it, especially to simple PRs.
Off-topic: an example that rewrites history might be nice for larger ones, if someone could share something that works well.
Things done
nix.conf? (See Nix manual)sandbox = relaxedsandbox = truenix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/)Add a 👍 reaction to pull requests you find important.